<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class publish_class extends AWS_MODEL
{
	public function update_attach($item_type, $item_id, $attach_access_key)
	{
		if (! $attach_access_key)
		{
			return false;
		}
		
		if ($this->update('attach', array(
			'item_id' => intval($item_id)
		), "item_type = '" . $this->quote($item_type) . "' AND item_id = 0 AND access_key = '" . $this->quote($attach_access_key) . "'"))
		{
			return $this->update($item_type, array('has_attach' => 1), $item_type . '_id = ' . intval($item_id));
		}
	}

	public function add_attach($item_type, $file_name, $attach_access_key, $add_time, $file_location, $is_image = false)
	{
		if ($is_image)
		{
			$is_image = 1;
		}
		
		return $this->insert('attach', array(
			'file_name' => htmlspecialchars($file_name), 
			'access_key' => $attach_access_key, 
			'add_time' => $add_time, 
			'file_location' => htmlspecialchars($file_location), 
			'is_image' => $is_image,
			'item_type' => $item_type
		));
	}

	public function remove_attach($id, $access_key)
	{
		$attach = $this->fetch_row('attach', "id = " . intval($id) . " AND access_key = '" . $this->quote($access_key) . "'");
		
		if (! $attach)
		{
			return false;
		}
		
		$this->delete('attach', "id = " . intval($id) . " AND access_key = '" . $this->quote($access_key) . "'");
		
		if (!$this->fetch_row('attach', 'item_id = ' . $attach['item_id']))
		{
			$this->update($attach['item_type'], array(
				'has_attach' => 0
			), $attach['item_type'] . '_id = ' . $attach['item_id']);
		}
		
		if ($attach['item_type'] == 'question')
		{
			$attach['item_type'] = 'questions';
		}
		
		foreach(AWS_APP::config()->get('image')->attachment_thumbnail AS $key => $val)
		{
			@unlink(get_setting('upload_dir').'/' . $attach['item_type'] . '/' . date('Ymd/', $attach['add_time']) . $val['w'] . 'x' . $val['h'] . '_' . $attach['file_location']);
		}
		
		@unlink(get_setting('upload_dir').'/' . $attach['item_type'] . '/' . date('Ymd/', $attach['add_time']) . $attach['file_location']);
		
		return true;
	}
	
	public function get_attach_by_id($id)
	{
		if ($data = $this->fetch_row('attach', 'id = ' . intval($id)))
		{
			if ($data['item_type'] == 'question')
			{
				$data['item_type'] = 'questions';
			}
		
			$result = array(
				'id' => $data['id'], 
				'is_image' => $data['is_image'], 
				'file_name' => $data['file_name'], 
				'access_key' => $data['access_key'], 
				'attachment' => get_setting('upload_url').'/' . $data['item_type'] . '/' . date('Ymd', $data['add_time']) . '/' . $data['file_location']
			);
			
			if ($data['is_image'] == 1)
			{
				$result['thumb'] = get_setting('upload_url').'/' . $data['item_type']. '/' . date('Ymd', $data['add_time']) . '/' . AWS_APP::config()->get('image')->attachment_thumbnail['min']['w'] . 'x' . AWS_APP::config()->get('image')->attachment_thumbnail['min']['h'] . '_' . $data['file_location'];
			}
			
			return $result;
		}
		
		return false;
	}
	
	public function get_attach($item_type, $item_id, $size = 'square')
	{
		$attach_list = array();

		if ($attach = $this->fetch_all('attach', "item_type = '" .  $this->quote($item_type). "' AND item_id = " . intval($item_id), "is_image DESC, id ASC"))
		{		
			foreach ($attach as $key => $data)
			{
				if ($item_type == 'question')
				{
					$item_type = 'questions';
				}
				
				$attach_list[$data['id']] = array(
					'id' => $data['id'], 
					'is_image' => $data['is_image'], 
					'file_name' => $data['file_name'], 
					'access_key' => $data['access_key'], 
					'attachment' => get_setting('upload_url').'/' . $item_type . '/' . date('Ymd', $data['add_time']) . '/' . $data['file_location'],
				);
				
				if ($data['is_image'] == 1)
				{
					$attach_list[$data['id']]['thumb'] = get_setting('upload_url').'/' . $item_type. '/' . date('Ymd', $data['add_time']) . '/' . AWS_APP::config()->get('image')->attachment_thumbnail[$size]['w'] . 'x' . AWS_APP::config()->get('image')->attachment_thumbnail[$size]['h'] . '_' . $data['file_location'];
				}
			}
		}
		
		return $attach_list;
	}

	public function get_file_class($file_type)
	{
		switch (strtolower($file_type))
		{
			case '3ds' :
				return 'file_Type_3ds';
				break;
			
			case 'zip' :
			case 'rar' :
			case 'gz' :
			case 'tar' :
			case 'cab' :
				return 'file_Type_zip';
				break;
			
			case 'ai' :
			case 'psd' :
			case 'cdr' :
				return 'file_Type_gif';
				break;
			
			default :
				return 'file_Type_txt';
				break;
		}
	}
	
	public function update_attach_statistics()
	{
		$questions = $this->query_all("SELECT question_id FROM " . $this->get_table('question'));
		
		foreach ($questions AS $key => $val)
		{
			if ($this->count('attach', "item_type = 'question' AND item_id = " . $val['question_id']))
			{
				$this->update('question', array('has_attach' => 1), 'question_id = ' . intval($val['question_id']));
			}
			else
			{
				$this->update('question', array('has_attach' => 0), 'question_id = ' . intval($val['question_id']));
			}
		}
		
		$answers = $this->query_all("SELECT answer_id FROM " . $this->get_table('answer'));
		
		foreach ($answers AS $key => $val)
		{
			if ($this->count('attach', "item_type = 'answer' AND item_id = " . $val['answer_id']))
			{
				$this->update('answer', array('has_attach' => 1), 'answer_id = ' . intval($val['answer_id']));
			}
			else
			{
				$this->update('answer', array('has_attach' => 0), 'answer_id = ' . intval($val['answer_id']));
			}
		}
		
		return true;
	}
}